package com.lzs.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lzs.PO.Menu;
import com.lzs.PO.User;
import com.lzs.Service.MenuService;
import com.lzs.Service.UserService;

@Controller
@RequestMapping(value = "/login")
public class LoginController {
	@Autowired
	private UserService userService;
	@Autowired
	private MenuService menuService;

	@RequestMapping(value = "/login_action", method = RequestMethod.POST)
	public String login(String select, String username, String password, Model model, HttpSession session,
			HttpServletRequest request) {

		User user = this.userService.check(username, password);

		if (user != null && user.getPassword().equals(password)) {

			session.setAttribute("user", user);

		// 获取用户权限信息
			
	   List<Menu> menus = this.menuService.queryMenus4User(user);
   
	   Map<String, Menu> map = new HashMap<String, Menu>();

       //Menu root = null;
	   List<Menu> rootlist = new ArrayList<Menu>();
	 
		for (Menu menu : menus) {

		map.put(menu.getMenuid(), menu);
		}
	   
		for (Menu menu : menus) {
         
		Menu child = menu;
		
		if (child.getPid().equals("0")) {
             rootlist.add(menu);
			//root = menu;				
		 } else {
			
			 	Menu parent = map.get(child.getPid());
			    System.out.println("父节点"+parent);
				parent.getChildren().add(child);
			
			}
	}
	  
			session.setAttribute("rootlist", rootlist);

			return "main2";

		}
		

		return "error";

	
	}
}
